package com.cssl.controller;

import com.cssl.dto.AddRegionDTO;
import com.cssl.dto.UpdateEquipmentDTO;
import com.cssl.entity.Equipment;
import com.cssl.entity.EquipmentRegion;
import com.cssl.exception.BusinessException;
import com.cssl.service.EquipmentRegionService;
import com.cssl.service.EquipmentService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RefreshScope
@RestController
@Tag(name = "上机控制器")
public class OrderController {
    @Autowired
    private EquipmentService es; //座位表

    @Autowired
    private EquipmentRegionService ers; //区域表

    /**
     * 获取所有座位
     * @return
     */
    @PostMapping("/getAllSeats")
    public List<Equipment> getAllSeats(){
        return es.findEquipment();
    }

    /**
     * 批量更新座位
     * @return
     */
    @PostMapping("/addEquipment")
    public boolean addEquipment(@RequestBody List<UpdateEquipmentDTO> listEq){
        System.out.println("addEquipment:"+listEq);
        try {
            es.batchUpdateEq(listEq);
            return true;
        } catch (BusinessException e) {
            return false;
        }
    }

    /**
     * 获取所有区域
     * @return
     */
    @PostMapping("/findRegion")
    public List<EquipmentRegion> findRegion(){
        return ers.list();
    }

    /**
     * 添加区域
     * @param regDto
     * @return
     */
    @PostMapping("/addRegion")
    public boolean addRegion(AddRegionDTO regDto){
        return ers.save(new EquipmentRegion(
                regDto.getRegion_lv(),
                regDto.getDisposition(),
                regDto.getRegion_price()
        ));
    }

    /**
     * 修改座位状态
     * @return
     */
    @PostMapping("/updateStatus")
    public boolean updateStatus(@RequestParam(name = "id") int id){
        return es.updateById(new Equipment(id,1));
    }
}
